//9.1 - The Mysterious Store - Mark Lee - Prima Publishing #include #include #include using namespace std; #define MAX(a,b) a inventory; vector forSale; int money; public: Store(Item* itemList, int n); ~Store() {} string BuyItem(int item); string viewInventory(); string ListItems(); int getMoney() { return MAX(money,0); } }; Store::Store(Item* itemList, int n) { for(int i = 0; i < n; i++) forSale.push_back(itemList[i]); money = 20; } string Store::BuyItem(int item) { money -= forSale[item-1].price; if (money < 0) return "\nSorry, you don't have enough money.\n\n"; inventory.push_back(forSale[item-1]); return "You bought a " + forSale[item-1].name + '\n'; } string Store::ListItems() { string s; for(int i = 0; i < forSale.size(); i++) { s += "["; s += i + 49; s+="]Buy a "; s+= forSale[i].name; s+= " ($"; s+= forSale[i].price + 48; s+= ")\n"; } return s; } string Store::viewInventory() { string s; for(int i = 0; i < inventory.size(); i++) s += inventory[i].name + '\n'; return s + '\n'; } int main(void) { int input; Item f[3]; f[0].name = "Clown"; f[0].price = 2; f[1].name = "Cracker Jack"; f[1].price = 6; f[2].name = "Camel"; f[2].price = 9; Store s = Store(f,3); while(true) { do { cout << "Welcome to the store.\n" << "You have " << s.getMoney() << " dollars.\n" << "\nWhat would you like to do?\n" << s.ListItems() << "[4]View your inventory\n" << "[5]Leave\n"; cin >> input; }while(input<1||input>5); switch(input) { case 4: cout << s.viewInventory(); break; case 5: goto END; default: cout << s.BuyItem(input); } } END: cout << "See ya!"; return 0; }